package com.dream.banana.notify.support;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.util.StringUtils;
import oshi.json.SystemInfo;
import oshi.json.hardware.HardwareAbstractionLayer;
/**
 * 躲了一辈子雨，雨会不会很难过。
 * </p>
 * Created by zhumenglong on 2017/5/28.
 */
public abstract class AbstractNotifyInitialization implements INotifyInitialization {
    protected boolean openNotify;
    protected boolean useDefaultConfig = false;
    protected String addresses;
    protected int port = 5672;
    protected String virtualHost = "/";
    protected String username;
    protected String password;
    protected String systemCode;
    protected int concurrentConsumers;
    protected int corePoolSize;
    protected int maxPoolSize;
    protected int queueCapacity;
    protected int keepAliveSeconds;
    protected static NotifyConfigSupport BASIC_CONFIG;

    protected abstract INotifyInitialization.NotifyTask initialization();

    protected void init() {
        if (this.openNotify) {
            if (this.useDefaultConfig) {
                BASIC_CONFIG = new NotifyConfigSupport();
            } else {
                NotifyConfig connConfig = new NotifyConfig();
                connConfig.setAddresses(this.addresses).setPort(this.port).setVirtualHost(this.virtualHost);
                connConfig.setUsername(this.username).setPassword(this.password);
                BASIC_CONFIG = new NotifyConfigSupport(connConfig);
            }

            this.initialization().run();
        }

    }

    protected String getProcessorId() {
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        return hal.getProcessor().getProcessorID();
    }

    public void setOpenNotify(boolean openNotify) {
        this.openNotify = openNotify;
    }

    public void setUseDefaultConfig(boolean useDefaultConfig) {
        this.useDefaultConfig = useDefaultConfig;
    }

    public void setAddresses(String addresses) {
        if (!this.useDefaultConfig) {
            this.addresses = StringUtils.isEmpty(addresses.replaceAll(" ", "")) ? null : addresses;
        }

    }

    public void setPort(String port) {
        if (!this.useDefaultConfig) {
            this.port = StringUtils.isEmpty(port.replaceAll(" ", "")) ? 5672 : Integer.parseInt(port);
        }

    }

    public void setVirtualHost(String virtualHost) {
        if (!this.useDefaultConfig) {
            this.virtualHost = StringUtils.isEmpty(virtualHost.replaceAll(" ", "")) ? this.virtualHost : virtualHost;
        }

    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSystemCode(String systemCode) {
        this.systemCode = systemCode;
    }

    public void setConcurrentConsumers(int concurrentConsumers) {
        this.concurrentConsumers = concurrentConsumers;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public void setQueueCapacity(int queueCapacity) {
        this.queueCapacity = queueCapacity;
    }

    public void setKeepAliveSeconds(int keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
    }

    public ConnectionFactory getConnectionFactory() {
        return BASIC_CONFIG.getConnectionFactory();
    }

    public RabbitAdmin getRabbitAdmin() {
        return BASIC_CONFIG.getRabbitAdmin();
    }

    public AmqpTemplate getAmqpTemplate() {
        return BASIC_CONFIG.getAmqpTemplate();
    }

    public MessageConverter getDefaultMessageConverter() {
        return BASIC_CONFIG.getDefaultMessageConverter();
    }
}
